#!/bin/bash
# 数组
# characters=("hidden")
characters="lp"


for file in /dev/usb/*;
do
    base_file=$(basename "$file")
    # 取文件名前两个字符
    prefix=${base_file:0:2}
    # 比较文件名前两个字符是否等于指定的字符

    # if [[ "$base_file" != "${characters[0]}" ]]; then
    if [ "$prefix" == "$characters" ]; then
        echo "Opening device: $base_file"
        nL=124  # 用您需要的具体数值替换
        nH=0   # 用您需要的具体数值替换

        #左对齐的指令集
        echo -ne '\x1B\x61\x00' > "$file"    
        #设置字体和字体大小
        echo -ne '\x1B\x4D\x00' > "$file"
        echo -ne '\x1D\x21\x01' > "$file"

        #打印抬头
        print_head=${19}
        printf " %s\n" "$print_head" | iconv -f UTF-8 -t GBK >> "$file"

        # 发送 ESC/P 指令
        #中间对齐的指令集
        echo -ne '\x1B\x61\x01' > "$file"
        #设置字体和字体大小
        echo -ne '\x1B\x4D\x00' > "$file"
        echo -ne '\x1D\x21\x01' > "$file"
        # 发送文本内容
        string="测试结果"
        printf " %s\n" "$string" | iconv -f UTF-8 -t GBK >> "$file"
        
        #字体大小
        echo -ne '\x1D\x21\x00' > "$file"

        #初始化打印机
        # echo -ne '\x1B\x40'  > "$file"
        string3="日差"
        string4="偏振"
        string5="摆幅"
        printf "    %4s    %4s   %4s\n"  "$string3" "$string4" "$string5"  |iconv -f UTF-8 -t GBK >> "$file"
        echo -ne '\x1B\x40'  > "$file"
        #设置左对齐
        echo -ne '\x1B\x61\x00' > "$file"
        # ./print.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 表匠校表仪
        printf "    C H: %-5.1f   %-4.1f   %-3s\n" "$1" "$2" "${3}" >> "$file"
        printf "    C B: %-5.1f   %-4.1f   %-3s\n" "$4" "$5" "${6}" >> "$file"
        printf "    6 H: %-5.1f   %-4.1f   %-3s\n" "$7" "$8" "${9}" >> "$file"
        printf "    9 H: %-5.1f   %-4.1f   %-3s\n" "${10}" "${11}" "${12}" >> "$file"
        printf "    12H: %-5.1f   %-4.1f   %-3s\n" "${13}" "${14}" "${15}" >> "$file"
        printf "    3 H: %-5.1f   %-4.1f   %-3s\n" "${16}" "${17}" "${18}" >> "$file"

        #设置字体和字体大小
        echo -ne '\x1B\x4D\x00'>"$file"
        echo -ne '\x1D\x21\x00' > "$file"

        string6="平均值"
        string7="增量值"
        string8="平均值(垂直-水平)"
        string9="日差平均值(6H-CH)"

        # groups=("$@")  # 读取传入的参数组
        # 计算 groups 数组中的元素个数并减去 1
        # groups_len=$(( ${#groups[@]} - 1 ))
        printf "%4s :"  "$string6" |iconv -f UTF-8 -t GBK >> "$file"
        printf " %-5.1f   %-4.1f   %-3s\n" "${20}" "${21}" "${22}" >> "$file"
        printf "%4s :"  "$string7" |iconv -f UTF-8 -t GBK >> "$file"
        printf " %-5.1f   %-4.1f   %-3s\n" "${23}" "${24}" "${25}" >> "$file"
        printf "%4s:\n"  "$string8" |iconv -f UTF-8 -t GBK >> "$file"
        printf "         %-4.1f          %-3s\n" "${26}" "${27}" >> "$file"
        printf "%4s:\n"  "$string9" |iconv -f UTF-8 -t GBK >> "$file"
        printf "         %-4.1f\n" "${28}">> "$file"


        printf "\n" >> "$file" 
        printf "\n" >> "$file" 
        printf "\n" >> "$file" 


        # Further operations if needed
    fi
done


# 蓝牙USB串口
if [ -e /dev/ttyACM0 ]; then
    owner_stat=$(stat -c '%U' /dev/ttyACM0)
    if [ "$owner_stat" == "bj" ]; then
        echo "The owner of /dev/ttyACM0 is bj (checked with stat)."
    else
        echo "The owner of /dev/ttyACM0 is not bj (checked with stat). It is $owner_stat."
        echo 1 | sudo -S chown bj /dev/ttyACM0
    fi

    file="/dev/ttyACM0"

    #左对齐的指令集
    echo -ne '\x1B\x61\x00' > "$file"    
    #设置字体和字体大小
    echo -ne '\x1B\x4D\x00' > "$file"
    echo -ne '\x1D\x21\x01' > "$file"

    #打印抬头
    print_head=${19}
    printf " %s\n" "$print_head" | iconv -f UTF-8 -t GBK >> "$file"

    # 发送 ESC/P 指令
    #中间对齐的指令集
    echo -ne '\x1B\x61\x01' > "$file"
    #设置字体和字体大小
    echo -ne '\x1B\x4D\x00' > "$file"
    echo -ne '\x1D\x21\x01' > "$file"
    # 发送文本内容
    string="测试结果"
    printf " %s\n" "$string" | iconv -f UTF-8 -t GBK >> "$file"
    
    #字体大小
    echo -ne '\x1D\x21\x00' > "$file"

    #初始化打印机
    # echo -ne '\x1B\x40'  > "$file"
    string3="日差"
    string4="偏振"
    string5="摆幅"
    printf "    %4s    %4s   %4s\n"  "$string3" "$string4" "$string5"  |iconv -f UTF-8 -t GBK >> "$file"
    echo -ne '\x1B\x40'  > "$file"
    #设置左对齐
    echo -ne '\x1B\x61\x00' > "$file"
    # ./print.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 表匠校表仪
    printf "    C H: %-5.1f   %-4.1f   %-3s\n" "$1" "$2" "${3}" >> "$file"
    printf "    C B: %-5.1f   %-4.1f   %-3s\n" "$4" "$5" "${6}" >> "$file"
    printf "    6 H: %-5.1f   %-4.1f   %-3s\n" "$7" "$8" "${9}" >> "$file"
    printf "    9 H: %-5.1f   %-4.1f   %-3s\n" "${10}" "${11}" "${12}" >> "$file"
    printf "    12H: %-5.1f   %-4.1f   %-3s\n" "${13}" "${14}" "${15}" >> "$file"
    printf "    3 H: %-5.1f   %-4.1f   %-3s\n" "${16}" "${17}" "${18}" >> "$file"

    #设置字体和字体大小
    echo -ne '\x1B\x4D\x00'>"$file"
    echo -ne '\x1D\x21\x00' > "$file"

    string6="平均值"
    string7="增量值"
    string8="平均值(垂直-水平)"
    string9="日差平均值(6H-CH)"

    # groups=("$@")  # 读取传入的参数组
    # 计算 groups 数组中的元素个数并减去 1
    # groups_len=$(( ${#groups[@]} - 1 ))
    printf "%4s :"  "$string6" |iconv -f UTF-8 -t GBK >> "$file"
    printf " %-5.1f   %-4.1f   %-3s\n" "${20}" "${21}" "${22}" >> "$file"
    printf "%4s :"  "$string7" |iconv -f UTF-8 -t GBK >> "$file"
    printf " %-5.1f   %-4.1f   %-3s\n" "${23}" "${24}" "${25}" >> "$file"
    printf "%4s:\n"  "$string8" |iconv -f UTF-8 -t GBK >> "$file"
    printf "         %-4.1f          %-3s\n" "${26}" "${27}" >> "$file"
    printf "%4s:\n"  "$string9" |iconv -f UTF-8 -t GBK >> "$file"
    printf "         %-4.1f\n" "${28}">> "$file"


    printf "\n" >> "$file" 
    printf "\n" >> "$file" 
    printf "\n" >> "$file" 

    # Further operations if needed

else
    echo "/dev/ttyACM0 does not exist (checked with test)" 
fi





